package com.ijunfu.webSocket;

import com.ijunfu.common.utils.JsonUtil;
import com.ijunfu.common.utils.R;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import java.io.IOException;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2024/2/6 23:31
 * @Version: 1.0
 * @Motto  : 简洁的代码是智慧的结晶 卓越的编码是对复杂性的优雅征服
 *
 */
@Slf4j
public class WsDecoder implements Decoder.Text<R> {

    @Override
    public R decode(String s) throws DecodeException {
        try {
            return JsonUtil.toObject(s, R.class);
        } catch (IOException e) {
            log.error("format websocket message fail: msg={} err={}", s, e.getMessage());

            throw new DecodeException(s, e.getMessage());
        }
    }

    @Override
    public boolean willDecode(String s) {


        return false;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
